package com.spzx.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.utils.DateUtils;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.SpzxService;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.user.domain.UserCollectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserCollectMapper;
import com.spzx.user.domain.UserCollect;
import com.spzx.user.service.IUserCollectService;

/**
 * 用户收藏Service业务层处理
 *
 * @author atguigu
 * @date 2024-08-19
 */
@Service
public class UserCollectServiceImpl extends ServiceImpl<UserCollectMapper, UserCollect> implements IUserCollectService
{
    @Autowired
    private UserCollectMapper userCollectMapper;

    @Autowired
    private SpzxService spzxService;



    /**
     * 查询用户收藏列表
     *
     * @param userCollect 用户收藏
     * @return 用户收藏
     */
    @Override
    public List<UserCollect> selectUserCollectList(UserCollect userCollect)
    {
        return userCollectMapper.selectUserCollectList(userCollect);
    }

    @Override
    public List<UserCollectVo> getUserCollectVoList() {
        Long userId = SecurityUtils.getUserId();
        List<UserCollect> userCollectList = userCollectMapper.selectList(new LambdaQueryWrapper<UserCollect>().eq(UserCollect::getUserId, userId));
        List<UserCollectVo> userCollectVoList = userCollectList.stream().map(userCollect -> {
            Long skuId = userCollect.getSkuId();
            ProductSku productSku = spzxService.getProductSku(skuId, SecurityConstants.INNER).getData();
            UserCollectVo userCollectVo = new UserCollectVo();
            userCollectVo.setSkuId(skuId);
            userCollectVo.setSkuName(productSku.getSkuName());
            userCollectVo.setSalePrice(productSku.getSalePrice());
            userCollectVo.setThumbImg(productSku.getThumbImg());
            userCollectVo.setCreateTime(new Date());
            return userCollectVo;
        }).collect(Collectors.toList());
        return userCollectVoList;
    }

}
